name: Release

on:
  push:
    tags:
      - "*"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Cache Build
        id: cache-build
        uses: actions/cache@v3
        with:
          path: rust/target
          key: ${{ runner.os }}-build

      - name: Cargo Test
        run: |
          cd rust
          cargo test

      - name: Cargo Clippy
        run: |
          cd rust
          cargo clippy -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used
        # cargo clippy --fix --allow-dirty -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used

      - name: Cargo Build Binary
        run: |
          cd rust
          cargo build --release

      - name: Optimize Binary with UPX
        run: |
          upx --best --lzma rust/target/release/telnum_gen

      - name: Package Binary
        run: |
          cd rust
          toolchain=$(rustup toolchain list | awk '{print $1;}')
          tar_name="telnum_gen_${{ github.ref_name }}-$toolchain"
          chmod +x target/release/telnum_gen
          tar -cvzf target/release/$tar_name.tar.gz target/release/telnum_gen

      - name: Release
        uses: docker://antonyurchenko/git-release:v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RELEASE_NAME_PREFIX: "Release "
          CHANGELOG_FILE: none
        with:
          args: |
            rust/target/release/*.tar.gz
